package com.aggron.concurrency.aqs;

import com.aggron.concurrency.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author LR2SD
 * @description CountDownLatch +await(10)测试类
 * @date 2018-06-12 21:04
 */
@ThreadSafe
@Slf4j
public class CountDownLatchExample2 {
    public static final int THREAD_COUNT=200;

    public static void main(String [] args) throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(THREAD_COUNT);
        ExecutorService executorService= Executors.newCachedThreadPool();
        for (int i = 0; i < THREAD_COUNT; i++) {
            final int  threadNum=i;
            executorService.execute(()->{
                try {
                    test(threadNum);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await(10, TimeUnit.MILLISECONDS);
        executorService.shutdown();
        log.info("finished");
    }

    private static void test(int threadNum) throws InterruptedException {
        Thread.sleep(100);
        log.info("{}",threadNum);

    }
}
